package com.jmall.user.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jmall.base.UserException;
import com.jmall.base.utils.SecurityUtils;
import com.jmall.user.dao.UserDAO;

@Service("userService")
@Transactional(rollbackFor=Exception.class)
public class UserService {
	@Resource(name="userDAO")
	private UserDAO userDAO = null;
	public Map checkLogin(String username,String password) throws UserException{
		List<Map> list = userDAO.findUserInfoByUsername(username);
		//判断用户是否存在
		if(list.size() == 0) {
			throw new UserException("用户不存在");
		}
		Map user = list.get(0);
		String md5Hex = SecurityUtils.toMD5Hex(password, user.get("salt").toString());
		//匹配密码是否正确
		if(!md5Hex.equals(user.get("password"))){ 
			throw new UserException("用户名或密码错误");
		}
		//判断用户状态
		if(!user.get("status").toString().equals("1")){
			throw new UserException("用户账户未启用");
		}
		return user;
	}
	public static void main(String[] args) throws UserException {
		
			ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
			UserService us = (UserService)ctx.getBean("userService");
			Map t = us.checkLogin("hb", "123456");
			System.out.println(t);
		}
	
}
